package com.doyens.auth.service;

import com.doyens.auth.client.UserClient;
import com.doyens.auth.config.JwtProperties;
import com.doyens.auth.entity.UserInfo;
import com.doyens.auth.utils.JwtUtils;
import com.doyens.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.swing.*;

@Service
public class AuthService {


    @Autowired
    private UserClient userClient;

    @Autowired
    private JwtProperties jwtProperties;


    public String accredit(String username, String password) {

        //1.根据用户名和密码查询 调用 doyens_user工程作用queryUser方法
      User userEntity = userClient.queryUser(username, password);
        //2.判断user
        if (userEntity ==null){
            return  null;
        }
        try {
            //3.使用jwtUtils生成token码进行返回
            UserInfo userInfo = new UserInfo();
            userInfo.setId(userEntity.getId());
            userInfo.setUsername(userEntity.getUsername());
           return  JwtUtils.generateToken(userInfo,this.jwtProperties.getPrivateKey(),this.jwtProperties.getExpire());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
